package forteresce.portprofile.profiles.util;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import forteresce.portprofile.config.constants.ProfileConstants;
import forteresce.portprofile.config.enums.SystemPropertiesEnum;

public class SystemUtil {
	
	private static Logger log = Logger.getLogger(SystemUtil.class);

	public static String getProfilePath() {
        String OS = SystemPropertiesEnum.OS_NAME.get();
        
        if (ProfileConstants.LINUX.equals(OS)) {
            return ProfileConstants.PROFILE_PATH_LINUX;
        } else if (OS.startsWith(ProfileConstants.WINDOWS)) {
        	return ProfileConstants.PROFILE_PATH_WINDOWS;
        } else if (OS.startsWith(ProfileConstants.MACOSX)) {
        	return ProfileConstants.PROFILE_PATH_MACOSX;
        }
        
        log.debug("No default profile path defined for OS: " + OS);
        
        return "";
	}
	
	public static String getProfileIniPath() {
		return getProfilePath() + ProfileConstants.PROFILE_INI_FILE_NAME;
	}

   /**
    * @return true if the profile specified is in use and false
    *         otherwise based on the lock file used by firefox
    **/
	public static boolean isProfileInUse(String profilePath) {
        String OS = SystemPropertiesEnum.OS_NAME.get();
        List<String> profileFiles = Arrays.asList((new File(profilePath)).list());
        
        if (ProfileConstants.LINUX.equals(OS)) {
             return profileFiles.contains(ProfileConstants.PROFILE_LOCK_FILE_LINUX);
        } else if (OS.startsWith(ProfileConstants.WINDOWS)) {
             return profileFiles.contains(ProfileConstants.PROFILE_LOCK_FILE_WINDOWS);
        } else if (OS.startsWith(ProfileConstants.MACOSX)) {
            return profileFiles.contains(ProfileConstants.PROFILE_LOCK_FILE_MACOSX);
        }
       
        log.debug("No lock file defined for OS: " + OS);
        
        return false;
    }
}
